USDA NASS Data

#Create a function for reading all of the crop files together
prePro<-function(crop){
  #read all of the files, using the crop name, and the .csv to create file list
  cropFile<-paste(crop,".csv", sep = "")
  #Return the crop file list
  return(cropFile)
}

State Crop Production Data from 1990-2021 in acres. The NASS data for each crop was downloaded by state, and trimmed into three columns in Microsoft Excel: Year, State, and Acres. The total land area of each state was noted in the GIS shapefiles. The square meter values were turned to acreages using the calculate function in an attribute table, and then the attribute table was exported to a .csv file for use in calculating the total crop area

# Develop a function to read in cropping history from 
# 1990-2021 (or whatever year is latest)
cropRead<-function(crop){
  #####
  ## This function is designed to take the long form ###  
  ## using year and state, and compresses it to the  ###  
  ## wide form, with a state having an individual    ###  
  ## line, then it calculates the TOTAL percentage   ###    
  ## land area of each crop and returns a data frame ###
  ##### 
  cropFile<-prePro(crop)
  #read in the input CSV file.
  cropLong<-read.csv(cropFile)
  #create an acre output column.
  cropLong$acres<-cropLong[,3]
  #Default values tend to be characters with commas. This  
  # line ensures that the data is transformed to numeric.
  cropLong$acres<-as.numeric(gsub(",", "", cropLong$acres))
  #Remove the initial character column
  cropLong[,3]<-NULL
  #Transfer the crop acreage from long format to wide
  cropWide<-spread(cropLong, key = Year, value = c(3))
  # The state Acres file was derived from GIS polygons  
  # from the Census Bureau. Square meters of land area  
  # was converted into acres in ArcMap 10.4.1
  stateAcres<-read.csv("STATEAcres.csv")
  # Make sure the state name is changed to uppercase
  stateAcres$NAME<-toupper(stateAcres$NAME)
  # Merge the state acres data with the crop wide data  
  # by state name, only keeping those that appear in the  
  # cropping wide matrix. 
  cropMerge<-merge(stateAcres, cropWide, by.x = 'NAME', 
                   by.y = 'State', all.y=TRUE)
  # Calculate the percentage of the total crop area  
  # produced per year divided by the acreage.
  percentages<-  cropMerge %>%
   mutate_at(vars(4:35) , funs(P =./cropMerge$AcreArea * 100))
  
  # Remove the raw acreage values and leave only the
  # percentages
  cropNew <- percentages[, c(1, 36:67)]
  # re-add commodity name0
  cropNew$crop<-crop
  return(cropNew)
}

Once the file is read in and transformed, with new percentages calculated, the data is linked to a second function that transforms the data into a matrix format for use in the creation of heatmaps and plots.

heatProcess<-function(cropFile){
  #####
  ## The objective of this function is to create the ##
  ## parameters necessary for creating the heatmap   ##
  ## by each individual state. It is designed to     ##
  ## take the long form using year and state, and    ##
  ## compresses it to the wide form.                 ##
  ######
  
  #read in each individual crop type
  cropNew<-cropRead(cropFile)
  # Use the states as row names
  # Remove the Name Variable
  cropNew$crop<-NULL
  #Remove all of the Other States parameters
  cropNew<-filter(cropNew, NAME != "OTHER STATES")
  #Replace all of the NA values with 0
  cropNew[is.na(cropNew)]<-0
  #reorder the data frame by the percentage of land from 1990
  cropNew<- cropNew[order(cropNew$`1990_P`),]
  #rename the columns by year
  colnames(cropNew)<-c("NAME", "'90", "'91", "'92", "'93","'94", "'95","'96", 
                       "'97","'98", "'99","'00", "'01", "'02", "'03", "'04", 
                       "'05", "'06", "'07", "'08", "'09", "'10", "'11", "'12", 
                       "'13","'14", "'15","'16", "'17","'18", "'19","'20","'21")
  #Convert the wide data frame into a long one via crop name
  longFrame<-melt(cropNew, id.vars=c("NAME"))
  #create the numerical values for the text display
  longFrame <- longFrame %>%
      #Add the state values and name
      mutate(text = paste0("State: ", NAME, "\n", 
                           #Add the year and year name
                           "Year: ", variable, "\n", 
                           #Add the percentages rounded to a value of 2 digits
                           "Percentage: ",round(value,2), "\n"))
  #Create a ggplot using the long frame data
  p<-  ggplot(longFrame, 
              #using the percentage (Variable), and state names
              aes(variable, NAME, 
              #Fill with percentages and add the text as a hover feature
              fill= value, text=text)) + 
    #Create a tile chart
    geom_tile() +
    #Reverse the scale of the axis
    scale_y_discrete(limits=rev) + 
    #Set the graident from low to high
    scale_fill_gradient(low="white", high="red") +
    #Remove the axis names and put the legend on the left
    theme(axis.title.x = element_blank(),
          axis.title.y = element_blank(), 
          legend.position="right") 
  #Convert the ggplot to plotyly
  finalPlot<-ggplotly(p, tooltip="text")
  #Return the final plot
  return(finalPlot)
}

And this function uses the data matrix from the above functions, and transfers the matrix dataframe to a graphical heat map based on the crop selected.

#create a megafile with all state and crop information
allCrops<-c('alfalfaHay', 'barley', 'canola', 'corn', 'cotton', 'drybeans', 
            'durumwheat', 'flaxseeds', 'hay', 'hops', 'lentils', 'oats', 
            'peanuts', 'peas', 'potatos', 'rice', 'sorghum', 'soybean', 
            'springwheat', 'sugarbeets', 'sugarcane', 'sunflower','sweetpotato', 
            'tobacco', 'winterwheat')

#Create a list of all of the crops and run it through the cropRead function
finalCropFrame<-lapply(X = allCrops, FUN = cropRead)

#Put all of the data together into one final crop frame
cropFrame<-data.frame(Reduce(rbind, finalCropFrame))

#Replace all NA values with 0
cropFrame[is.na(cropFrame)]<-0
#STATE name values are in all capital letters
stateCrop<-function(state){
  
  #####
  ## The objective of this function is to create the ##
  ## parameters necessary for creating the heatmap   ##
  ## by each individual crop type by state. It is    ##
  ## designed to take the long form using year and   ##   
  ## state, and compresses it to the wide form.      ##
  ######
  #Take the large crop frame values and change the name
  stateNew<-cropFrame
  #set the state name as the state inputted in the function
  stateName<-state
  #Filter the state by statename
  filterState<-filter(stateNew,NAME==stateName)
  #Remove the state name
  filterState$NAME<-NULL
  #Change the column names to reflect the year and the crop type
  colnames(filterState)<-c("'90", "'91", "'92", "'93","'94", "'95","'96", "'97",
                           "'98", "'99","'00", "'01", "'02", "'03", "'04", "'05",
                           "'06", "'07", "'08", "'09", "'10", "'11", "'12", 
                           "'13","'14", "'15","'16", "'17","'18", "'19", "'20",
                           "'21", "crop")
  #Set the crop value as a factor
  levels(as.factor(filterState$crop))
  #Rename the crop type for display purposes
  filterState$crop <- fct_recode(filterState$crop, "Alfalfa" = "alfalfaHay", 
                                 "Barley" = "barley", "Canola"= "canola", 
                                 "Corn" = "corn", "Cotton" = "cotton", 
                                 "Dry Beans" = "drybeans", "Durum Wheat" = "durumwheat", 
                                 "Flax" = "flaxseeds", "Hay" = "hay", 
                                 "Hops" = "hops", "Lentils" = "lentils", 
                                 "Oats" = "oats", "Peanuts" = "peanuts", 
                                 "Peas" = "peas", "Potatoes" = "potatos", 
                                 "Rice" = "rice", "Sorghum" = "sorghum", 
                                 "Soybean" = "soybean", "Spring Wheat" = "springwheat", 
                                 "Sugar Beets" = "sugarbeets", "Sugar Cane"="sugarcane", 
                                 "Sunflower" = "sunflower", "Sweet Potato" = "sweetpotato", 
                                 "Tobacco" = "tobacco", "Winter Wheat" = "winterwheat")
  #Transfer the data frame into the long format by crop type
  longFrame<-melt(filterState, id.vars=c("crop"))
  #order the values alphabetically
  longFrame<- longFrame[order(longFrame$crop),]
  #create the values for the text display
  longFrame <- longFrame %>%
     #Add the crop name
     mutate(text = paste0("Crop: ", crop, "\n",
                          #Add the year
                          "Year: ", variable, "\n", 
                          #Add the percentage
                          "Percentage: ",round(value,2), "\n"))
  #Create a ggplot
  p<-  ggplot(longFrame, 
              #using the percentage (Variable), and state names
              aes(variable, crop, 
                  #Fill with percentages and add the text as a hover feature
                  fill= value, text=text)) + 
    #Create a tile chart
    geom_tile() +
    #Reverse the scale of the axis
    scale_y_discrete(limits=rev) + 
    #Set the graident from low to high
    scale_fill_gradient(low="white", high="red") +
    #Remove the axis names and put the legend on the left
    theme(axis.title.x = element_blank(),
          axis.title.y = element_blank(), 
          legend.position="right") 
  #Convert to plotly
  finalPlot<-ggplotly(p, tooltip="text")
  #return the plotly
  return(finalPlot)
}

Key State Information

This file can be downloaded, and the state of choice can be changed using the stateCrop function, and a query of capital letters.

North Dakota

North Dakota is a very unique state in regards to commodities. There are many different crops that grow only in North Dakota. Over time, since the 1990s, there has been a decline in small grain production, including barley, Durum and spring Wheat, and oat production, as well as sunflower, in favor of an increased production of soybeans and corn, and to a lesser extent, canola.

#Run North Dakota through the state crop function
ND<-stateCrop("NORTH DAKOTA")
#Display it
ND

Wisconsin

Wisconsin used to be a state that produced a lot alfalfa as a legume, as well as a decline in hay production. This crop has been replaced over this 20 year period by soybeans. Corn production has been steady in Wisconsin.

#Repeat for Wisconsin
WI<-stateCrop("WISCONSIN")
WI

Iowa

Iowa has been mostly steady in areas of corn and soy production (being in the corn belt), with the small amount of hay and alfalfa being replaced with soybean. Unlike North Dakota, the state isn’t known for its diversity.

#Repeat for Iowa
IA<-stateCrop("IOWA")
IA

Kansas

Kansas, like North Dakota, is another state that has had their primary crop (winter wheat) replaced by corn and soybean production. Sorghum and hay production has been steady, as has sunflower production.

#Repeat for Kansas
KS<-stateCrop("KANSAS")
KS

Major Cash Crops

Corn

Corn is one of the most prominent cash crops in the United States, though Illinois, Iowa, and Indiana comprise the largest production of the crop (over 25% of land area), and production seems to hold steady there. There has been expansion in many other states adjacent to the corn belt, like Nebraska, Kansas, and Minnesota. Ohio, Wisconsin, Delaware, and Maryland are other large area producers, and seemed to be holding steady over time.

Soybean

Soybean is not produced in as many states as corn. However, in most of the states, soybean area has increased. In the corn belt, production of soybean area has increased. Illinois, Indiana, Iowa, Minnesota, Nebraska, Wisconsin, and both Dakotas.

Cotton

Cotton, overall, has been on the decline nationally, with Texas and Georgia the only state holding steady.

Rice

Rice is not grown in a huge part of the country, and production has been steady, with a slight decline in Mississippi.

Wheat

Winter Wheat

Winter wheat area has, overall, declined nationally. With Kansas and Oklahoma being the biggest producers, with their overall area cut in half.

Spring Wheat

Spring wheat is also on decline, with North and South Dakota being the primary producers, and the overall area of production cut in half.

Durum Wheat

There has also been a large decline in area of Durum wheat, especially in the primary state of production (North Dakota).

Other Grains

Oats

Oat production in the United States is down, overall, over a 20 year time period, especially in areas that it used to grow in.

Barley

Like all other small grains, barley area is down over a 20 year time period.

Flaxseed

Flaxseed production has remained steady, though, it really has only been produced in North Dakota, with a slight increase in Montana.

Sorghum

Sorghum production in Kansas and Texas has remained steady, as it is they are the primary producer of sorghum in the US. Nebraska, Missouri, and Arkansas have seen a drop off in production.

Oil Crops

All Sunflower

Production of Sunflower in South Dakota is up, with most other states producing sunflowers experiencing a decline.

Canola

Canola is a crop primarly grown in North Dakota, with the state experiencing and increase in cropland area.

Sugar Crops

Sugar Beets

Sugar Beets are grown in the Upper midwest and plains, and production has been mostly the same in these areas over 20 years.

Sugarcane (Harvested Sugar and Seed)

Sugarcane fields have also been steady in the three states that it is produced in.

Potatoes

Potatoes

Idaho and Washington have experienced a steady potato production over 20 years, while North Dakota and Delaware have experienced a decline in production.

Sweet Potatoes (No 2021 Data)

Sweet potato production has increased in North Carolina and Mississippi, with a decline in production in Louisiana.

Pulses and Beans

Dry Beans

Dry Peas (And Austrian Peas)

Lentils

Peanuts

Other Crops

Tobacco (Harvested Acres ONLY)

Hops

Hay

All Hay

Alfalfa Hay